home *** CD-ROM | disk | FTP | other *** search
/ Aminet 41 / Aminet 41 (2001)(Schatztruhe)[!][Feb 2001].iso / Aminet / dev / c / libiconv_src.lha / tests / table-to.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-11-07  |  2.6 KB  |  89 lines

  1. /* Copyright (C) 2000 Free Software Foundation, Inc.
  2.    This file is part of the GNU ICONV Library.
  3.  
  4.    The GNU ICONV Library is free software; you can redistribute it and/or
  5.    modify it under the terms of the GNU Library General Public License as
  6.    published by the Free Software Foundation; either version 2 of the
  7.    License, or (at your option) any later version.
  8.  
  9.    The GNU ICONV Library is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12.    Library General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU Library General Public
  15.    License along with the GNU ICONV Library; see the file COPYING.LIB.  If not,
  16.    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  17.    Boston, MA 02111-1307, USA.  */
  18.  
  19. /* Create a table from Unicode to CHARSET. */
  20.  
  21. #include <stddef.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <iconv.h>
  25. #include <errno.h>
  26.  
  27. int main (int argc, char* argv[])
  28. {
  29.   const char* charset;
  30.   iconv_t cd;
  31.  
  32.   if (argc != 2) {
  33.     fprintf(stderr,"Usage: table-to charset\n");
  34.     exit(1);
  35.   }
  36.   charset = argv[1];
  37.  
  38.   cd = iconv_open(charset,"UCS-4-INTERNAL");
  39.   if (cd == (iconv_t)(-1)) {
  40.     perror("iconv_open");
  41.     exit(1);
  42.   }
  43.  
  44.   {
  45.     unsigned int i;
  46.     unsigned char buf[10];
  47.     for (i = 0; i < 0x10000; i++) {
  48.       unsigned int in = i;
  49.       const char* inbuf = (const char*) ∈
  50.       size_t inbytesleft = sizeof(unsigned int);
  51.       char* outbuf = (char*)buf;
  52.       size_t outbytesleft = sizeof(buf);
  53.       size_t result = iconv(cd,&inbuf,&inbytesleft,&outbuf,&outbytesleft);
  54.       if (result == (size_t)(-1)) {
  55.         if (errno != EILSEQ) {
  56.           int saved_errno = errno;
  57.           fprintf(stderr,"0x%02X: iconv error: ",i);
  58.           errno = saved_errno;
  59.           perror("");
  60.           exit(1);
  61.         }
  62.       } else if (result == 0) /* ignore conversions with transliteration */ {
  63.         unsigned int j, jmax;
  64.         if (inbytesleft != 0 || outbytesleft == sizeof(buf)) {
  65.           fprintf(stderr,"0x%02X: inbytes = %ld, outbytes = %ld\n",i,(long)(sizeof(unsigned int)-inbytesleft),(long)(sizeof(buf)-outbytesleft));
  66.           exit(1);
  67.         }
  68.         jmax = sizeof(buf) - outbytesleft;
  69.         printf("0x");
  70.         for (j = 0; j < jmax; j++)
  71.           printf("%02X",buf[j]);
  72.         printf("\t0x%04X\n",i);
  73.       }
  74.     }
  75.   }
  76.  
  77.   if (iconv_close(cd) < 0) {
  78.     perror("iconv_close");
  79.     exit(1);
  80.   }
  81.  
  82.   if (ferror(stdin) || ferror(stdout)) {
  83.     fprintf(stderr,"I/O error\n");
  84.     exit(1);
  85.   }
  86.  
  87.   exit(0);
  88. }
  89.